import 'dart:io';

main() async {
  print('main start');
//  getNetworkData('arg').then((value) => print(value));

  getData();
  // await getData();
  print('main end');
}

/**
 * 解决两个问题:
 *  1.await必须在async函数中才能使用
 *  2.async 函数返回必须是个Future
 *
 */
//Future getNetworkData() async {
//  await sleep(Duration(seconds: 2));
//  return 'Hello Dart';
//}

Future getData() async {
//  getNetworkData('arg').then((value) {
//    print(value);
//    return getNetworkData(value);
//  }).then((value) {
//    print(value);
//    return getNetworkData(value);
//  }).then((value) => print(value));

  var re1 = await getNetworkData('arg');
  print(re1);
  var re2 = await getNetworkData(re1);
  print(re2);
  var re3 = await getNetworkData(re2);
  print(re3);
}

Future<String> getNetworkData(arg) {
  return Future<String>(() {
    sleep(Duration(seconds: 2));
    return 'Hello Dart:$arg';
  });
}
